package org.sample.dining.starve;

import org.sample.dining.AbstractChopstick;
import org.sample.dining.AbstractPhilosopher;

/**
 * Created by alan on 2017/7/12.
 */
public class Philosopher extends AbstractPhilosopher {

    public Philosopher(String name, AbstractChopstick left, AbstractChopstick right) {
        super(name, left, right);
    }

    @Override
    public boolean useChopsticks() {
        boolean result = false;
        if (left.take()) {
            if (right.take()) {
                int leftIndex = left.getIndex();
                int rightIndex = right.getIndex();

                System.out.println(name + " 使用筷子" + leftIndex + "," + rightIndex);

                result = true;
                right.drop();
            }
            left.drop();
        }
        return result;
    }
}